package co.yixiang.huiMin.controller;


import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.huiMin.dao.MachinistQueryCriteria;
import co.yixiang.huiMin.mapper.ClassifyMapper;
import co.yixiang.huiMin.entity.Classify;
import co.yixiang.huiMin.entity.Machinist;
import co.yixiang.huiMin.repository.MachinistRepository;
import co.yixiang.huiMin.service.MachinistService;
import co.yixiang.modules.shop.domain.YxStoreCategory;
import co.yixiang.modules.shop.repository.YxStoreCategoryRepository;
import co.yixiang.modules.shop.service.YxStoreCategoryService;
import co.yixiang.modules.shop.service.YxUserService;
import co.yixiang.modules.shop.service.dto.YxStoreCategoryDTO;
import co.yixiang.modules.shop.service.dto.YxUserDTO;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 机修师 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-07-01
 */
@RestController
@Api(tags = "机修师(改)")
@RequestMapping("/machinist")
public class MachinistController {

    @Resource
    private MachinistService machinistService;

    @Resource
    private YxUserService yxUserService;

    @Resource
    private ClassifyMapper classifyMapper;

    @Resource
    private MachinistRepository machinistRepository;

    @Resource
    private YxStoreCategoryService yxStoreCategoryService;

    @Resource
    private YxStoreCategoryRepository yxStoreCategoryRepository;
    /**
     * 查询机修师
     * @return
     */
    @AnonymousAccess
    @ApiOperation("查询机修师")
    @GetMapping("/selectYxUserWithdraws")
    public ResponseEntity<Object> selectYxUserWithdraws(Pageable pageable){


        MachinistQueryCriteria machinistQueryCriteria = new MachinistQueryCriteria();
        Page<Machinist> machinists = machinistRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, machinistQueryCriteria, criteriaBuilder), pageable);
        List<Machinist> list = machinists.getContent();
        for (Machinist machinist : list) {

            YxUserDTO yxUser=yxUserService.findById(machinist.getUserId());
            if(yxUser.getNickname()!=null){
                machinist.setNickname(yxUser.getNickname());
            }
            if(yxUser.getAvatar()!=null){
                machinist.setAvatar(yxUser.getAvatar());
            }
            //查询分类
//            YxStoreCategory yxStoreCategory = yxStoreCategoryRepository.findNameById(machinist.getClassifyId());
            YxStoreCategoryDTO yxStoreCategory=yxStoreCategoryService.findById(machinist.getClassifyId());
            if(machinist.getClassifyId()!=null){
                machinist.setClassifyName(yxStoreCategory.getCateName());
            }
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        resultMap.put("message", PageUtil.toPage(machinists));
        return new ResponseEntity<>(resultMap, HttpStatus.OK);

    }

    /**
     *机修师审核
     * @return
     */
    @AnonymousAccess
    @ApiOperation("机修师审核")
    @PostMapping("/updateMachinists")
    public ResponseEntity<Object> updateMachinistsYes(@RequestBody Machinist machinist){
        int count=machinistService.updateMachinists(machinist);
        HashMap<String, Object> resultMap = new HashMap<>();

        if(count>0){
            resultMap.put("code",200);
            return new ResponseEntity<>(resultMap, HttpStatus.OK);
        }
        resultMap.put("code",300);
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

}

